package com.example.zlx.service.impl;

import com.example.zlx.entity.User;
import com.example.zlx.repository.UserRepository;
import com.example.zlx.service.UserService;
import com.example.zlx.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceIpml implements UserService{

    @Autowired
    private UserRepository userRepository;

    @Override
    public ServerResponse<User> login(String username, String password) {
        User user=userRepository.findByUsernameAndPassword(username, password);
        if(user==null){
            return ServerResponse.createByErrorMessage("用户名或密码错误");
        }
        return ServerResponse.createBySuccess("登录成功",user);
    }

    @Override
    public ServerResponse<String> register(User user) {
//        try{
            userRepository.save(user);
            return ServerResponse.createBySuccessMessage("注册成功");
//        }catch (Exception e){
//            return ServerResponse.createByErrorMessage("注册失败");
//        }
    }
}
